package com.boot.security.controller;

import com.boot.security.model.dto.RegisterAccount;
import com.boot.security.model.vo.AccountInfo;
import com.boot.security.service.authentication.AccountService;
import com.boot.security.service.authentication.TokenService;
import com.boot.web.http.HttpModel;
import com.boot.web.http.Status;
import com.boot.web.util.HttpResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 账号的前端控制器
 * @date 2019/11/25 20:47
 **/

@Slf4j
@RestController
@RequestMapping("/account")
@Api("账户接口")
public class AccountController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private AccountService accountService;

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 更新账户绑定电话
     * @date 2020/6/20 20:09
     **/
    @PostMapping(value = "/register")
    @ApiOperation(value = "注册")
    @ApiImplicitParam(name = "body", value = "账号密码", required = true, paramType = "body", dataTypeClass = RegisterAccount.class)
    public ResponseEntity<HttpModel<Object>> register(@RequestBody RegisterAccount registerAccount) {
        if (accountService.register(registerAccount)) {
            return HttpResponseUtil.CREATED("账号创建成功");
        }
        return HttpResponseUtil.badRequest("账号创建失败");
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 更新账户信息与Authorization
     * @date 2020/3/8 15:31
     **/
    @PutMapping("/authorization")
    @ApiOperation(value = "更新账户和认证信息")
    public ResponseEntity<HttpModel<AccountInfo>> upgradeAuthorization() {
        AccountInfo oldInfo = accountService.getAccountInformation();
        AccountInfo accountInfo = accountService.upgradeAccountInformation(oldInfo);
        String token = tokenService.build(accountInfo);
        return ResponseEntity.status(HttpStatus.CREATED)
                .header(TokenService.TOKEN, token)
                .header(TokenService.UPGRADE, String.valueOf(false))
                .body(HttpModel.<AccountInfo>builder().data(accountInfo).build());
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 获取账户信息
     * @date 2020/3/8 15:31
     **/
    @GetMapping("/information")
    @ApiOperation(value = "获取账户信息")
    public ResponseEntity<HttpModel<AccountInfo>> getAccountInformation() {
        return HttpResponseUtil.OK("获取账户信息成功", accountService.getAccountInformation());
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 更新账户密码
     * @date 2020/6/20 20:09
     **/
    @PutMapping(value = "/password")
    @ApiOperation(value = "更新账户密码")
    @ApiImplicitParam(name = "body", value = "新旧密码", required = true, paramType = "body", dataTypeClass = Map.class)
    public ResponseEntity<HttpModel<Object>> upgradePassword(@RequestBody Map<String, String> body) {
        if (accountService.upgradePassword(
                body.get("oldPassword"),
                body.get("newPassword"))) {
            return tokenUpgrade();
        }
        return HttpResponseUtil.badRequest("更新账户密码失败");
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 更新账户绑定电话
     * @date 2020/6/20 20:09
     **/
    @PutMapping(value = "/phone")
    @ApiOperation(value = "更新账户绑定电话")
    @ApiImplicitParam(name = "body", value = "密码和新电话", required = true, paramType = "body", dataTypeClass = Map.class)
    public ResponseEntity<HttpModel<Object>> upgradePhone(@RequestBody Map<String, String> body) {
        if (accountService.upgradePhone(
                body.get("oldPhone"),
                body.get("newPhone"))) {
            return tokenUpgrade();
        }
        return HttpResponseUtil.badRequest("更新账户绑定电话失败");
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 账户电话解绑
     * @date 2020/6/22 15:51
     **/
    @DeleteMapping(value = "/phone")
    @ApiOperation(value = "账户电话解绑")
    public ResponseEntity<HttpModel<Object>> deletePhone() {
        if (accountService.deletePhone()) {
            return tokenUpgrade();
        }
        return HttpResponseUtil.badRequest("账户电话解绑失败");
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 更新账户绑定邮箱
     * @date 2020/6/20 20:10
     **/
    @PutMapping(value = "/email")
    @ApiOperation(value = "更新账户绑定邮箱")
    @ApiImplicitParam(name = "body", value = "密码和新邮箱", required = true, paramType = "body", dataTypeClass = Map.class)
    public ResponseEntity<HttpModel<Object>> upgradeEmail(@RequestBody Map<String, String> body) {
        if (accountService.upgradeEmail(
                body.get("oldEmail"),
                body.get("newEmail"))) {
            return tokenUpgrade();
        }
        return HttpResponseUtil.badRequest("更新账户绑定邮箱失败");
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 账户邮箱解绑
     * @date 2020/6/22 15:52
     **/
    @DeleteMapping(value = "/email")
    @ApiOperation(value = "账户邮箱解绑")
    public ResponseEntity<HttpModel<Object>> deleteEmail() {
        if (accountService.deleteEmail()) {
            return tokenUpgrade();
        }
        return HttpResponseUtil.badRequest("账户邮箱解绑失败");
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 更新账户头像
     * @date 2020/6/16 20:59
     **/
    @PutMapping(value = "/avatar")
    @ApiOperation(value = "更新账户头像")
    @ApiImplicitParam(name = "avatar", value = "头像", required = true, dataTypeClass = MultipartFile.class)
    public ResponseEntity<HttpModel<String>> upgradeAvatar(@RequestParam("avatar") MultipartFile avatar) {
        String fileName = accountService.upgradeAvatar(avatar);
        if (fileName != null) {
            return ResponseEntity.status(HttpStatus.OK)
                    .header(TokenService.UPGRADE, String.valueOf(true))
                    .body(HttpModel.<String>builder().code(Status.TOKEN_WARN.getStatus()).msg(Status.TOKEN_WARN.getDescription()).data(fileName).build());
        }
        return HttpResponseUtil.badRequest("更新账户头像失败");
    }

    private ResponseEntity<HttpModel<Object>> tokenUpgrade() {
        return ResponseEntity.status(HttpStatus.OK)
                .header(TokenService.UPGRADE, String.valueOf(true))
                .body(HttpModel.<Object>builder().code(Status.TOKEN_WARN.getStatus()).msg(Status.TOKEN_WARN.getDescription()).build());
    }
}
